home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / PEAR / Command / Common.php < prev    next >
PHP Script  |  2004-10-01  |  8KB  |  249 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Sµther Bakken <ssb@php.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Common.php,v 1.24 2004/01/08 17:33:13 sniper Exp $
  20.  
  21. require_once "PEAR.php";
  22.  
  23. class PEAR_Command_Common extends PEAR
  24. {
  25.     // {{{ properties
  26.  
  27.     /**
  28.      * PEAR_Config object used to pass user system and configuration
  29.      * on when executing commands
  30.      *
  31.      * @var object
  32.      */
  33.     var $config;
  34.  
  35.     /**
  36.      * User Interface object, for all interaction with the user.
  37.      * @var object
  38.      */
  39.     var $ui;
  40.  
  41.     var $_deps_rel_trans = array(
  42.                                  'lt' => '<',
  43.                                  'le' => '<=',
  44.                                  'eq' => '=',
  45.                                  'ne' => '!=',
  46.                                  'gt' => '>',
  47.                                  'ge' => '>=',
  48.                                  'has' => '=='
  49.                                  );
  50.  
  51.     var $_deps_type_trans = array(
  52.                                   'pkg' => 'package',
  53.                                   'extension' => 'extension',
  54.                                   'php' => 'PHP',
  55.                                   'prog' => 'external program',
  56.                                   'ldlib' => 'external library for linking',
  57.                                   'rtlib' => 'external runtime library',
  58.                                   'os' => 'operating system',
  59.                                   'websrv' => 'web server',
  60.                                   'sapi' => 'SAPI backend'
  61.                                   );
  62.  
  63.     // }}}
  64.     // {{{ constructor
  65.  
  66.     /**
  67.      * PEAR_Command_Common constructor.
  68.      *
  69.      * @access public
  70.      */
  71.     function PEAR_Command_Common(&$ui, &$config)
  72.     {
  73.         parent::PEAR();
  74.         $this->config = &$config;
  75.         $this->ui = &$ui;
  76.     }
  77.  
  78.     // }}}
  79.  
  80.     // {{{ getCommands()
  81.  
  82.     /**
  83.      * Return a list of all the commands defined by this class.
  84.      * @return array list of commands
  85.      * @access public
  86.      */
  87.     function getCommands()
  88.     {
  89.         $ret = array();
  90.         foreach (array_keys($this->commands) as $command) {
  91.             $ret[$command] = $this->commands[$command]['summary'];
  92.         }
  93.         return $ret;
  94.     }
  95.  
  96.     // }}}
  97.     // {{{ getShortcuts()
  98.  
  99.     /**
  100.      * Return a list of all the command shortcuts defined by this class.
  101.      * @return array shortcut => command
  102.      * @access public
  103.      */
  104.     function getShortcuts()
  105.     {
  106.         $ret = array();
  107.         foreach (array_keys($this->commands) as $command) {
  108.             if (isset($this->commands[$command]['shortcut'])) {
  109.                 $ret[$this->commands[$command]['shortcut']] = $command;
  110.             }
  111.         }
  112.         return $ret;
  113.     }
  114.  
  115.     // }}}
  116.     // {{{ getOptions()
  117.  
  118.     function getOptions($command)
  119.     {
  120.         return @$this->commands[$command]['options'];
  121.     }
  122.  
  123.     // }}}
  124.     // {{{ getGetoptArgs()
  125.  
  126.     function getGetoptArgs($command, &$short_args, &$long_args)
  127.     {
  128.         $short_args = "";
  129.         $long_args = array();
  130.         if (empty($this->commands[$command])) {
  131.             return;
  132.         }
  133.         reset($this->commands[$command]);
  134.         while (list($option, $info) = each($this->commands[$command]['options'])) {
  135.             $larg = $sarg = '';
  136.             if (isset($info['arg'])) {
  137.                 if ($info['arg']{0} == '(') {
  138.                     $larg = '==';
  139.                     $sarg = '::';
  140.                     $arg = substr($info['arg'], 1, -1);
  141.                 } else {
  142.                     $larg = '=';
  143.                     $sarg = ':';
  144.                     $arg = $info['arg'];
  145.                 }
  146.             }
  147.             if (isset($info['shortopt'])) {
  148.                 $short_args .= $info['shortopt'] . $sarg;
  149.             }
  150.             $long_args[] = $option . $larg;
  151.         }
  152.     }
  153.  
  154.     // }}}
  155.     // {{{ getHelp()
  156.     /**
  157.     * Returns the help message for the given command
  158.     *
  159.     * @param string $command The command
  160.     * @return mixed A fail string if the command does not have help or
  161.     *               a two elements array containing [0]=>help string,
  162.     *               [1]=> help string for the accepted cmd args
  163.     */
  164.     function getHelp($command)
  165.     {
  166.         $config = &PEAR_Config::singleton();
  167.         $help = @$this->commands[$command]['doc'];
  168.         if (empty($help)) {
  169.             // XXX (cox) Fallback to summary if there is no doc (show both?)
  170.             if (!$help = @$this->commands[$command]['summary']) {
  171.                 return "No help for command \"$command\"";
  172.             }
  173.         }
  174.         if (preg_match_all('/{config\s+([^\}]+)}/e', $help, $matches)) {
  175.             foreach($matches[0] as $k => $v) {
  176.                 $help = preg_replace("/$v/", $config->get($matches[1][$k]), $help);
  177.             }
  178.         }
  179.         return array($help, $this->getHelpArgs($command));
  180.     }
  181.  
  182.     // }}}
  183.     // {{{ getHelpArgs()
  184.     /**
  185.     * Returns the help for the accepted arguments of a command
  186.     *
  187.     * @param  string $command
  188.     * @return string The help string
  189.     */
  190.     function getHelpArgs($command)
  191.     {
  192.         if (isset($this->commands[$command]['options']) &&
  193.             count($this->commands[$command]['options']))
  194.         {
  195.             $help = "Options:\n";
  196.             foreach ($this->commands[$command]['options'] as $k => $v) {
  197.                 if (isset($v['arg'])) {
  198.                     if ($v['arg']{0} == '(') {
  199.                         $arg = substr($v['arg'], 1, -1);
  200.                         $sapp = " [$arg]";
  201.                         $lapp = "[=$arg]";
  202.                     } else {
  203.                         $sapp = " $v[arg]";
  204.                         $lapp = "=$v[arg]";
  205.                     }
  206.                 } else {
  207.                     $sapp = $lapp = "";
  208.                 }
  209.                 if (isset($v['shortopt'])) {
  210.                     $s = $v['shortopt'];
  211.                     @$help .= "  -$s$sapp, --$k$lapp\n";
  212.                 } else {
  213.                     @$help .= "  --$k$lapp\n";
  214.                 }
  215.                 $p = "        ";
  216.                 $doc = rtrim(str_replace("\n", "\n$p", $v['doc']));
  217.                 $help .= "        $doc\n";
  218.             }
  219.             return $help;
  220.         }
  221.         return null;
  222.     }
  223.  
  224.     // }}}
  225.     // {{{ run()
  226.  
  227.     function run($command, $options, $params)
  228.     {
  229.         $func = @$this->commands[$command]['function'];
  230.         if (empty($func)) {
  231.             // look for shortcuts
  232.             foreach (array_keys($this->commands) as $cmd) {
  233.                 if (@$this->commands[$cmd]['shortcut'] == $command) {
  234.                     $command = $cmd;
  235.                     $func = @$this->commands[$command]['function'];
  236.                     if (empty($func)) {
  237.                         return $this->raiseError("unknown command `$command'");
  238.                     }
  239.                     break;
  240.                 }
  241.             }
  242.         }
  243.         return $this->$func($command, $options, $params);
  244.     }
  245.  
  246.     // }}}
  247. }
  248.  
  249. ?>